import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class TCPCODE {
    public static void main(String[] args) throws IOException {
        //1. 获取socket
        Socket socket = new Socket(InetAddress.getByName("localhost"), 8888);

        //2. 获取io
        OutputStream outputStream = socket.getOutputStream();
        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        //3. 发送数据
        String message;
        InputStream inputStream = socket.getInputStream();

        while ((message = bufferedReader.readLine()) != null) {
            printWriter.println(message);
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            System.out.println("receive: " + new String(bytes, 0, len));

        }
        outputStream.close();
        socket.close();
    }
}
